package cn.edu.cqupt.cxcy.listener;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import cn.edu.cqupt.cxcy.dto.AdminImpl;

public class SessionListener implements HttpSessionListener {

	/**
     * 当前所有用户会话集合
     */
	private HashMap<String,HttpSession> sessionMap=new HashMap<String, HttpSession>();
	
	@Override
	public void sessionCreated(HttpSessionEvent se) {
//		 logger.info("====>创建新session");
	        Object sessionMap=se.getSession().getServletContext().getAttribute("sessionMap");
	        if(sessionMap==null){
	            se.getSession().getServletContext().setAttribute("sessionMap",this.sessionMap);
	        }
	        this.sessionMap.put(se.getSession().getId(), se.getSession());
//	        logger.info("====>创建session完成");
	}

	@SuppressWarnings("unchecked")
	@Override
	public void sessionDestroyed(HttpSessionEvent se) {
//		logger.info("====>销毁session");
        /**
         * 用户离线操作
         */
		new AdminImpl().SessionUpdata( (String) ((List<Object>) se.getSession().getAttribute("user")).get(4), false);
        this.sessionMap.remove(se.getSession().getId());      
//        logger.info("====>销毁session完成");
    }
    public HashMap<String, HttpSession> getSessionMap() {
        return sessionMap;
    }
    public void setSessionMap(HashMap<String, HttpSession> sessionMap) {
        this.sessionMap = sessionMap;
    }

}
